package org.zjt.platform.demo.config;

import javax.servlet.http.HttpServletRequest;

import org.zjt.platform.demo.config.properties.WebProperties;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.method.annotation.ResponseEntityExceptionHandler;


/**
 * @author <a href="juntao.zhang@qq.com">juntao.zhang</a>
 * @Description: controller 异常处理
 * @Package org.zjt.platform.demo.config
 * @date 2018/3/12 11:41
 * @see
 */
@ControllerAdvice(annotations = {Controller.class})
@Slf4j
public class ControllerExceptionHandler extends ResponseEntityExceptionHandler {

    @Autowired
    private WebProperties webProperties;

	@ExceptionHandler
    ModelAndView handleException(HttpServletRequest request, Throwable ex) {
        String errorMsg=ex.getMessage()==null ? "NullPointerException": ex.getMessage();
        ModelAndView mv=new ModelAndView(webProperties.getErrView());
        mv.addObject("errorMsg",errorMsg);
        log.error("执行controller异常:",ex);
        return mv;
    }


}
